package com.tebiecloud.order.server.controller.feign;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.tebiecloud.common.model.ResultBody;
import com.tebiecloud.common.utils.ResultBodyUtils;
import com.tebiecloud.common.utils.StringUtils;
import com.tebiecloud.order.client.model.entity.Reserve;
import com.tebiecloud.order.client.model.vo.ReserveList;
import com.tebiecloud.order.client.service.IReserveServiceClient;
import com.tebiecloud.order.server.service.ReserveService;
import org.assertj.core.util.Arrays;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/feign/reserve")
public class ReserveReserveController implements IReserveServiceClient {

    @Autowired
    ReserveService reserveService;

    @PostMapping("selectReserveList")
    @Override
    public ResultBody<List<Reserve>> selectReserveList(
            @RequestParam(value = "storeId",required = false) Long storeId,
            @RequestParam(value = "userId",required = false) Long userId,
            @RequestParam(value = "statusList",required = false) String statusList) {
        QueryWrapper<Reserve> reserveQueryWrapper = new QueryWrapper<>();
        if(storeId!=null)reserveQueryWrapper.eq("store_id",storeId);
        if(userId!=null)reserveQueryWrapper.eq("user_id",userId);
        if(StringUtils.isNotEmpty(statusList)){
            String[] str = statusList.split(",");
            List<Object> statusArray = Arrays.asList(str);
            reserveQueryWrapper.in("status",statusArray);
        }
        reserveQueryWrapper.orderByAsc("number");
        List<Reserve> reserveList = reserveService.list(reserveQueryWrapper);
        return ResultBodyUtils.ok(reserveList);
    }

    @PostMapping("update")
    @Override
    public ResultBody<Reserve> update(Long reserveId, Integer status) {
        UpdateWrapper<Reserve> reserveUpdateWrapper = new UpdateWrapper<>();
        reserveUpdateWrapper.eq("reserve_id",reserveId);
        Reserve reserve = new ReserveList();
        reserve.setReserveId(reserveId);
        reserve.setStatus(status);
        reserve.setShutTime(new Date());
        reserveService.updateById(reserve);
        return ResultBodyUtils.ok(reserve);
    }

    @PostMapping("noticeReserve")
    @Override
    public ResultBody noticeReserve(Long storeId) {
        reserveService.noticeReserve(storeId);
        return ResultBodyUtils.ok();
    }
}
